clear all
*cap log close
set more off
set seed 603


// files to be read in --------
local NAMEIN1 = "history_q2"
local NAMEIN2 = "exper_q2"
local NAMEOUT = "table_subgroups_q2"
// -----------------------------------------------------


* first table: using quadratic estimates ------------------------------------------
local NAMEIN1 = "history_q2"
local NAMEIN2 = "exper_q2"
local NAMEOUT = "table_subgroups_q2"

//// Initialize LaTeX Table ---------------------
capture erase "${out}/apx_extrap/`NAMEOUT'.tex"
file open fh using "${out}/apx_extrap/`NAMEOUT'.tex", write replace

file write fh ///	
	"&\multicolumn{4}{c}{\$Y_0\$} && \multicolumn{4}{c}{\$Y_1 - Y_0\$}\\" _n /// 
	"\cmidrule(l){2-5} \cmidrule(l){7-10}" _n ///
	"& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8)\\" _n ///
	"& All & \$D=1\$ & \$D=0\$ & Diff && All & \$D=1\$ & \$D=0\$ & Diff \\" _n ///
	"&     &         &         &      && (ATE) & (ATT) & (ATU) &  \\" _n ///
	"\hline" _n
	
	
// First part: driving history -------
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel A: By motorist offense history (prior two years)}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN1'.csv", clear
replace par = trim(upper(par))
local glab1 = "No offense"
local glab2 = "Any offense"

foreach q in 1 2 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	


// Second part: officer experience -------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel B: By officer experience}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN2'.csv", clear
keep if yvar == "cite_ny1"
replace g = g+1
replace par = trim(upper(par))
local glab1 = "Low"
local glab2 = "High"

foreach q in 1 2 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	


// Third part: officer experience (contesting-------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel C: By officer experience (\$Y=\$ Pr(contest))}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN2'.csv", clear
keep if yvar == "contest"
replace g = g+1
replace par = trim(upper(par))
local glab1 = "Low"
local glab2 = "High"

foreach q in 1 2 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	


file write fh "\bottomrule" _n  
file close fh
macro drop fh
* ---------------------------------------------------------------------------------



* second table: using tail mean estimates -----------------------------------------
local NAMEIN1 = "history_q2"
local NAMEIN2 = "exper_q2"
local NAMEOUT = "table_subgroups_q2"

local NAMEIN1 = "history_b5"
local NAMEIN2 = "exper_b5"
local NAMEOUT = "table_subgroups_b5"
// -----------------------------------------------------



//// Initialize LaTeX Table ---------------------
capture erase "${out}/apx_extrap/`NAMEOUT'.tex"
file open fh using "${out}/apx_extrap/`NAMEOUT'.tex", write replace

file write fh ///	
	"&\multicolumn{4}{c}{\$Y_0\$} && \multicolumn{4}{c}{\$Y_1 - Y_0\$}\\" _n /// 
	"\cmidrule(l){2-5} \cmidrule(l){7-10}" _n ///
	"& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8)\\" _n ///
	"& All & \$D=1\$ & \$D=0\$ & Diff && All & \$D=1\$ & \$D=0\$ & Diff \\" _n ///
	"&     &         &         &      && (ATE) & (ATT) & (ATU) &  \\" _n ///
	"\hline" _n
	
	
// First part: driving history -------
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel A: By motorist offense history (prior two years)}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN1'.csv", clear
replace par = trim(upper(par))
local glab1 = "No offense"
local glab2 = "Any offense"

foreach q in 1 2 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	


// Second part: officer experience -------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel B: By officer experience}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN2'.csv", clear
keep if yvar == "cite_ny1"
replace g = g+1
replace par = trim(upper(par))
local glab1 = "Low"
local glab2 = "High"

foreach q in 1 2 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	


// Third part: officer experience (contesting-------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel C: By officer experience (\$Y=\$ Pr(contest))}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN2'.csv", clear
keep if yvar == "contest"
replace g = g+1
replace par = trim(upper(par))
local glab1 = "Low"
local glab2 = "High"

foreach q in 1 2 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	



file write fh "\bottomrule" _n  
file close fh
macro drop fh
* ---------------------------------------------------------------------------------









